import os 
import cv2


def move_file(path,to_path,start,end,new_start):
    
    for i in range(start,end+1):
        new_path = os.path.join(path,str(i))
        save_path = os.path.join(to_path, str(i+new_start))
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        for f in os.listdir(new_path):
            file_ = os.path.join(new_path,f)
            img = cv2.imread(file_)
            cv2.imwrite(os.path.join(save_path,f),img)
        
def make_label(path):
    with open(os.path.join(path , 'label.txt'),'w') as f:
        labels = os.listdir(path)
        for l in labels:
            new_path = os.path.join(path, l)
            if os.path.isdir(new_path):
                for p in os.listdir(new_path):
                    f.write(l + "/" + p + " " + l + '\n')

def rename_dir_func(rename_dir,start_num):
    files = [os.path.join(rename_dir,p) for p in os.listdir(rename_dir)]

    for idx,f in enumerate(files):
        new_path = os.path.join(rename_dir,str(idx+start_num)+".jpg")
        if os.path.exists(new_path):
            print("exist: ", new_path)
            print(f)
        else :
            os.rename(f, new_path)


def rename_dir_1(path,num,end):
    count = num

    for i in range(num,end +1):
        new_path = os.path.join(path,str(i) + '.png')
        if not os.path.exists(new_path):
            continue
        else :
            os.rename(new_path,os.path.join(path,str(count)  + '.png') )

            count +=1

def rename_name_1(path,num,end):   #文件名前面加数字
    count = num
    files = [os.path.join(path,p) for p in os.listdir(path)]
    

    for i in files:
        if os.path.isfile(i):
            name = str(count) + 'X' + os.path.basename(i)
        
            os.rename(i,os.path.join(path,name) )

            count +=1


def rename_name(path,num,end):   #文件名格式改为数字格式
    count = num
    files = [os.path.join(path,p) for p in os.listdir(path)]
    

    for i in files:
        if os.path.isfile(i):
            name = os.path.basename(i).split('.')[-1]
        
            os.rename(i,os.path.join(path,str(count) + '.' + name) )

            count +=1

if __name__ == '__main__':

    # path = r'D:\bugs\gq4'

    # to_path = 'E:\\caffe-master\\my\\OCR\\2\\test'
    # to_path1 = 'E:\\caffe-master\\my\\OCR\\2\\train'
        
    # start = 3954
    # move_file(path,to_path1,0,40,start)

    # make_label(to_path)
    # make_label(to_path1)

    # path = r'C:\Users\vanlance\Desktop\char\char\84_1'
    # num  = 0

    # rename_name_1(path,0,206)

    path = "/media/swls/disk1/vanlance/project/electric_meter_box/img/20191107/2020-03-02/img3"
    rename_dir_func(path, 6000)
